home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_06 / 1106092b < prev    next >
Text File  |  1993-04-06  |  5KB  |  172 lines

  1.      /*******************************************
  2.      *
  3.      *   edge_gray_shade_region(..
  4.      *
  5.      *   This function segments an image by
  6.      *   growing gray shade regions inside of
  7.      *   edges.  It combines the techniques
  8.      *   of the edge_region and gray_shade_region
  9.      *   functions.
  10.      *
  11.      *   The steps are:
  12.      *      . detect edges
  13.      *      . threshold edge output to a
  14.      *        percent value
  15.      *      . lay the edges on top of the original
  16.      *        image to eliminate them from
  17.      *        consideration
  18.      *      . grow regions
  19.      *
  20.      *******************************************/
  21.  
  22. edge_gray_shade_region(in_name, out_name, the_image,
  23.             out_image, il, ie, ll, le, edge_type,
  24.             min_area, max_area, diff, percent,
  25.             set_value, erode)
  26.    char     in_name[], out_name[];
  27.    float    percent;
  28.    int      edge_type, il, ie, ll, le;
  29.    short    diff, erode, 
  30.             max_area, min_area, 
  31.             set_value,
  32.             the_image[ROWS][COLS],
  33.             out_image[ROWS][COLS];
  34. {
  35.    int    a, b, count, i, j, k,
  36.           length, width;
  37.    short  cutoff;
  38.    struct tiff_header_struct image_header;
  39.    unsigned long histogram[GRAY_LEVELS+1];
  40.  
  41.    if(does_not_exist(out_name)){
  42.       printf("\n\n output file does not exist %s",
  43.              out_name);
  44.       read_tiff_header(in_name, &image_header);
  45.       round_off_image_size(&image_header,
  46.                            &length, &width);
  47.       image_header.image_length = length*ROWS;
  48.       image_header.image_width  = width*COLS;
  49.       create_allocate_tiff_file(out_name, &image_header,
  50.                                 out_image);
  51.    }  /* ends if does_not_exist */
  52.  
  53.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  54.  
  55.       /***************************
  56.       *
  57.       *   Detect the edges.  Do
  58.       *   not threshold.
  59.       *
  60.       ****************************/
  61.  
  62.    if(edge_type == 1  ||
  63.       edge_type == 2  ||
  64.       edge_type == 3)
  65.       detect_edges(in_name, out_name, the_image,
  66.                    out_image, il, ie, ll, le,
  67.                    edge_type, 0, 0);
  68.  
  69.    if(edge_type == 4){
  70.       quick_edge(in_name, out_name, the_image,
  71.                  out_image, il, ie, ll, le,
  72.                  0, 0);
  73.    }  /* ends if 4 */
  74.    if(edge_type == 5){
  75.       homogeneity(in_name, out_name, the_image,
  76.                  out_image, il, ie, ll, le,
  77.                  0, 0);
  78.    }  /* ends if 5 */
  79.  
  80.    if(edge_type == 6){
  81.       difference_edge(in_name, out_name, the_image,
  82.                  out_image, il, ie, ll, le,
  83.                  0, 0);
  84.    }  /* ends if 6 */
  85.  
  86.    if(edge_type == 7){
  87.       contrast_edge(in_name, out_name, the_image,
  88.                  out_image, il, ie, ll, le,
  89.                  0, 0);
  90.    }  /* ends if 7 */
  91.  
  92.    if(edge_type == 8){
  93.       gaussian_edge(in_name, out_name, the_image,
  94.                  out_image, il, ie, ll, le,
  95.                  3, 0, 0);
  96.    }  /* ends if 8 */
  97.  
  98.    if(edge_type == 10){
  99.       range(in_name, out_name, the_image,
  100.             out_image, il, ie, ll, le,
  101.             3, 0, 0);
  102.    }  /* ends if 10 */
  103.  
  104.    if(edge_type == 11){
  105.       variance(in_name, out_name, the_image,
  106.                out_image, il, ie, ll, le,
  107.                0, 0);
  108.    }  /* ends if 11 */
  109.  
  110.       /* copy out_image to the_image */
  111.    for(i=0; i<ROWS; i++)
  112.       for(j=0; j<COLS; j++)
  113.          the_image[i][j] = out_image[i][j];
  114.  
  115.       /******************************
  116.       *
  117.       *   Threshold the edge detector
  118.       *   output at a given percent.
  119.       *   This eliminates the weak
  120.       *   edges.
  121.       *
  122.       *******************************/
  123.  
  124.    zero_histogram(histogram);
  125.    calculate_histogram(the_image, histogram);
  126.    find_cutoff_point(histogram, percent, &cutoff);
  127.    threshold_image_array(the_image, out_image,
  128.                          255, cutoff, set_value);
  129.  
  130.    if(erode != 0){
  131.          /* copy out_image to the_image */
  132.       for(i=0; i<ROWS; i++)
  133.          for(j=0; j<COLS; j++)
  134.             the_image[i][j] = out_image[i][j];
  135.       erode_image_array(the_image, out_image,
  136.                         set_value, erode);
  137.    }  /* ends if erode */
  138.  
  139.       /*******************************
  140.       *
  141.       *   Read the original gray shade
  142.       *   image back into the_image.
  143.       *
  144.       *******************************/
  145.  
  146.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  147.  
  148.       /*******************************
  149.       *
  150.       *   Overlay the edge values
  151.       *   on top of the original
  152.       *   image by setting them to
  153.       *   FORGET_IT so the region
  154.       *   growing will not use those
  155.       *   points.
  156.       *
  157.       *******************************/
  158.  
  159.    for(i=0; i<ROWS; i++)
  160.       for(j=0; j<COLS; j++)
  161.          if(out_image[i][j] == set_value)
  162.             the_image[i][j] = FORGET_IT;
  163.  
  164.    pixel_grow(the_image, out_image, diff,
  165.               min_area, max_area);
  166.  
  167.    write_array_into_tiff_image(out_name, out_image,
  168.                                il, ie, ll, le);
  169.  
  170. }  /* ends edge_gray_shade_region */
  171.  
  172.